package com.huodongjia.service;

import com.huodongjia.mapper.ActivityMapper;
import com.huodongjia.mapper.CreateMapper;
import com.huodongjia.mapper.JoinMapper;
import com.huodongjia.pojo.Activity;
import com.huodongjia.pojo.Create;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class ActivityService {
    @Autowired
    ActivityMapper activityMapper;
    @Autowired
    CreateMapper createMapper;
    @Autowired
    JoinMapper joinMapper;
    public List<Activity> findAll() {
        return activityMapper.findAll();
    }

    public List<Activity> search(String search) {
        return activityMapper.search(search);
    }

    public Activity findById(int activityId) {
        return activityMapper.findById(activityId);
    }

    public List<Activity> findByUserid(int userid) {
        List<Create> creates = createMapper.findByUserid(userid);
        List<Activity> activities = new ArrayList<>();
        for (Create create:creates){
            activities.add(activityMapper.findById(create.getActivityId()));
        }
        return activities;
    }

    public void delete(int activityId) {
        activityMapper.delete(activityId);
        joinMapper.deleteByActivity(activityId);
        createMapper.deleteByActivity(activityId);
    }
}
